package main.java.tmi.stockIn.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import main.java.tmi.stockIn.service.StockInService;
import main.java.tmi.stockIn.service.StockInVO;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

/**
 * Test Service Implementation
 *
 * @author ssong
 * @since 2010. 09. 10.
 */
@Service
public class StockInServiceImpl implements StockInService {
	
	// Logger
	private Log logger;
	
	@Resource(name = "stockInDAO")
	private StockInDAO stockInDao;
	
	/**
	 * The Constructor
	 */
	public StockInServiceImpl() {
		logger = LogFactory.getLog(this.getClass());
	}
	
	//조회
	@Override
	public Map<String,String> getStockInView(Map<String,String> param) throws Exception {

		return (Map<String,String>)stockInDao.getStockInView(param);

		//return resultIdx > 0 ? true : false;
	}
	

	@Override
	public List<StockInVO> getStockInDetailList(Map<String,String> param) throws SQLException {
		List<StockInVO> list = stockInDao.getStockInDetailList(param);
		System.out.println("====================="+list);
		return list;
	}
	
	//저장
	/*@Override
	public void addStockIn(Map<String, String> param, HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub

		stockInDao.addStockIn(param);

		//패킹 상세 		
		String batchNoTmp[] = request.getParameterValues("batchNo");
		String expireDateTmp[] 	  = request.getParameterValues("expireDate");
		String ordSeqTmp[] 	  = request.getParameterValues("ordSeq");
		String goodsCodeTmp[] 	  = request.getParameterValues("goodsCode");
		String qtyTmp[] 	  = request.getParameterValues("qty");
		
		System.out.println("===========>ordSeqTmp.length: "+ ordSeqTmp.length);
		if(ordSeqTmp.length > 0){
			Map detailParamMap = new HashMap();
			for (int i=0 ; i < ordSeqTmp.length ; i++){
				
				if ( StringUtils.isNotEmpty(ordSeqTmp[i]) ){
					detailParamMap.clear();	
					System.out.println("===========>ordSeqTmp[i]: "+ ordSeqTmp[i]);			
					detailParamMap.put("ordNo1", param.get("ordNo1"));
					detailParamMap.put("ordNo2", param.get("ordNo2"));
					detailParamMap.put("ordPseq", param.get("ordPseq"));
					detailParamMap.put("mawb", param.get("mawb"));
					detailParamMap.put("hawb", param.get("hawb"));
					detailParamMap.put("ordSeq", ordSeqTmp[i]);
					detailParamMap.put("batchNo", batchNoTmp[i]);
					detailParamMap.put("expireDate", expireDateTmp[i].replaceAll("-", "") );
					detailParamMap.put("qty", qtyTmp[i]);
					detailParamMap.put("goodsCode", goodsCodeTmp[i]);
					detailParamMap.put("sesseionId", param.get("sesseionId") );
					stockInDao.addStockInDetail(detailParamMap);
				}
			}
		}
	}*/
	
	

	//수정
	@Override
	public boolean modifyStockIn(Map<String, String> param, HttpServletRequest request) throws Exception {
		int resultCnt = 0;
		
		resultCnt = stockInDao.modifyStockIn(param);

		//상세 		
				
				String ordSeqTmp[] 	    = request.getParameterValues("ordSeq");
				String clearSeqTmp[]    = request.getParameterValues("clearSeq");
				
				String damQtyTmp[]      = request.getParameterValues("damQty");
				String lossQtyTmp[]     = request.getParameterValues("lossQty");
/*				String damLossStrTmp[]  = request.getParameterValues("damLossStr");*/
				String receiverNameTmp[]  = request.getParameterValues("receiverName");
				
				String qurYnTmp[]  = request.getParameterValues("qurYn");
				String goodsYnTmp[]  = request.getParameterValues("goodsYn");
				String goodsNaTmp[]  = request.getParameterValues("goodsNa");
				
				
				if(clearSeqTmp.length > 0 && damQtyTmp != null){
					Map detailParamMap = new HashMap();
					for (int i=0 ; i < clearSeqTmp.length ; i++){
						
						if ( StringUtils.isNotEmpty(clearSeqTmp[i]) ){
							detailParamMap.clear();	
							System.out.println("===========>clearSeqTmp[i]: "+ clearSeqTmp[i]);		
							
							detailParamMap.put("ordNo1", param.get("ordNo1"));
							detailParamMap.put("ordNo2", param.get("ordNo2"));
							detailParamMap.put("ordPseq", param.get("ordPseq"));
							detailParamMap.put("mawb", param.get("mawb"));
							detailParamMap.put("hawb", param.get("hawb"));
							detailParamMap.put("clearNo", param.get("clearNo"));
	                        detailParamMap.put("ordSeq", ordSeqTmp[i]);
							detailParamMap.put("clearSeq", clearSeqTmp[i]);

							detailParamMap.put("damQty", damQtyTmp[i]);
							detailParamMap.put("lossQty", lossQtyTmp[i]);
/*							detailParamMap.put("damLossStr",StringUtils.defaultString(damLossStrTmp[i],""));*/
							detailParamMap.put("receiverName",StringUtils.defaultString(receiverNameTmp[i],""));
							
							
							detailParamMap.put("qurYn", qurYnTmp[i]);
							detailParamMap.put("goodsYn", goodsYnTmp[i]);			
							
							detailParamMap.put("sesseionId", param.get("sesseionId") );
							
							resultCnt += stockInDao.stockInDetailUpdate(detailParamMap);
						}
					}
				}

		return resultCnt > 0 ? true : false;
	}
	
	//수정
	@Override
	public boolean stockInDetailUpdate(Map<String, String> param) throws Exception {
		int resultCnt ;
		resultCnt = stockInDao.stockInDetailUpdate(param);
		return resultCnt > 0 ? true : false;
	}
	

	//삭제
	@Override
	public boolean deleteDo(Map<String, String> param) throws Exception {
		int resultCnt ;
		stockInDao.deleteDoDep(param);
		resultCnt = stockInDao.deleteDoHead(param);
		return resultCnt > 0 ? true : false;
	}
}
